const http = require('http')
const hostname = '127.0.0.1';
const port = 3030;

function startServer() {

    const server = http.createServer((req, res) => {
        res.statusCode = 200
        res.setHeader('Content-Type', 'text/html') //若产生乱码，请参考http协议，修订这里
            // res.end('你好世界\n') //加上utf-8不乱码
        var htmlFile = '<!DOCTYPE html>' +
            '<html lang="en">' +
            '<head>' +
            '<meta charset="UTF-8">' +
            '<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
            '<title>Document</title>' +
            '</head>' +
            '<body>' +
            '<h1>hello world!</h1>' +
            '</body>' +
            '</html>'
        res.end(htmlFile)
    })

    server.listen(port, hostname, () => {
        console.log(`服务器运行在 http://${hostname}:${port}/`); //注意这里的模板字符
    })
}
exports.startServer = startServer